package com.ware.controller.admin;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.ware.controller.BaseController;
import com.ware.dao.ComConnection;
import com.ware.util.AjaxResult;
import com.ware.util.Convert;
import com.ware.validator.ConnectionValidator;

public class ComConnectionController extends BaseController {

	@Before(ConnectionValidator.class)
	public void edit() {
		AjaxResult result = new AjaxResult(1);

		try {
			ComConnection cn = this.getModel(ComConnection.class, "comInfo");
			if (cn.get("id", "").equals("")) {
				String createTime = Convert.getDateTimeString();
				cn.set("create_time", createTime).save();
				result.setData("id", cn.get("id"));
			} else {
				cn.update();
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.addError(e.getMessage());
		}
		this.renderJson(result.toJson());
	}

	public void info() {
		String sql = "SELECT * FROM com_connection";
		Record com = Db.findFirst(sql);
		if(com==null){
			this.setAttr("comInfo", com);
		}else{
			this.setAttr("comInfo", com.toJson());
			
		}
		this.render("connection.ftl");
	}
}
